﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Semantics.Silverlight;

namespace SilverlightParser
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        ServiceReference1.ParserServiceClient client;
        private void buttonParse_Click(object sender, RoutedEventArgs e)
        {
            this.client = new ServiceReference1.ParserServiceClient();
            this.client.ParseCompleted += new EventHandler<ServiceReference1.ParseCompletedEventArgs>(client_ParseCompleted);
            this.client.ParseAsync(this.textBoxSentence.Text);
        }

        void client_ParseCompleted(object sender, ServiceReference1.ParseCompletedEventArgs e)
        {
            try
            {
                if (e.Result == null)
                {
                    MessageBox.Show("Parse failed with no result(reuslt=null)!");
                    return;
                }
                ParseResult result = e.Result;
                switch (result.ResultState)
                {
                    case ParseResult.EnumResultState.Ambiguities:
                        ParserWindow win = new ParserWindow(result,this.textBoxSentence.Text,this.client);
                        win.Show();                        
                        break;
                    case ParseResult.EnumResultState.Exception:
                        MessageBox.Show("服务异常!\r\n" + result.Exception);
                        break;
                    case ParseResult.EnumResultState.Success:
                        ParserWindow win2 = new ParserWindow(result,this.textBoxSentence.Text,this.client);
                        win2.Show();                        
                        break;
                }
            }
            catch (System.Reflection.TargetInvocationException exp)
            {
                MessageBox.Show("服务错错误!");
                return;
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message, exp.GetType().ToString(), MessageBoxButton.OK);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                this.client = new ServiceReference1.ParserServiceClient();
                this.client.GetSentencesCompleted += new EventHandler<ServiceReference1.GetSentencesCompletedEventArgs>(client_GetSentencesCompleted);
                this.client.GetSentencesAsync();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message, exp.GetType().ToString(), MessageBoxButton.OK);
            }
        }
        ObservableCollection<Sentence> Sentences;
        void client_GetSentencesCompleted(object sender, ServiceReference1.GetSentencesCompletedEventArgs e)
        {
            this.Sentences = e.Result;
            this.dataGridSentences.ItemsSource = this.Sentences;
            Sentence s = this.Sentences[0];
        }

        private void dataGridSentences_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Sentence sentence = (Sentence)this.dataGridSentences.SelectedItem;
            this.textBoxSentence.Text = sentence.Words;
        }
    }
}
